<?php
class FacebookUtility
{
    public static function getUserId()
    {
        return Yii::app()->session['fb_user_id'];
    }
    
    public static function destroySession()
    {
        unset(Yii::app()->session['fb_user_id']);
    }

    public static function getAccessToken()
    {
        
    }

    public static function setAccessToken($token)
    {

    }

    public static function setExtendedAccessToken()
    {

    }

    public static function getUserInfo($userId)
    {
        // gender
        $rand = rand(1, 100);
        if($rand <= 40)
        {
            $gender = 'female';
        }
        elseif(41 <= $rand && $rand <= 80)
        {
            $gender = 'male';
        }
        else
        {
            $gender = null;
        }
        
        // birthday
        $birthday = date('m/d/Y', mt_rand(strtotime('1973-01-01'), strtotime('1998-12-31')));

        // avatar
        $avatar = PhotoUtility::getUrl().'/520052/ffffff.jpg&text=u%s_1';
        $avatar = sprintf($avatar, $userId);
        
        // place id
        $config = PhpData::read('location');
        $location = $config[array_rand($config)];
        $hometown = $config[array_rand($config)];

        // work
        $config = PhpData::read('work');
        shuffle($config);
        $works = array_slice($config, 0, mt_rand(0, 5));

        // education
        $config = PhpData::read('education');
        shuffle($config);
        $educations = array_slice($config, 0, mt_rand(0, 5));

        // result
        return array
        (
            'name' => 'name_'.$userId,
            'first_name' => 'first_'.$userId,
            'last_name' => 'last_'.$userId,
            'profile_url' => 'http://www.facebook.com/user_'.$userId,
            'username' => 'username_'.$userId,
            'email' => 'email_'.$userId.'@facebook.com',
            'sex' => $gender,
            'birthday_date' => $birthday,
            'pic_big' => $avatar,
            'current_location' => $location,
            'hometown_location' => $hometown,
            'work' => $works,
            'education' => $educations
        );
    }
    
    public static function getLocation($id)
    {
        $config = PhpData::read('location');
        $location = $config[$id];
        return array
        (
            'name' => $location['location_name'],
            'latitude' => $location['latitude'],
            'longitude' => $location['longitude']
        );
    }
    
    public static function getAlbumProfile($userId)
    {
        $url = PhotoUtility::getUrl().'/520052/ffffff.jpg&text=u%s_%s';
        $result = array();
        for($i = 2; $i <= 6; $i++)
        {
            $result[] = array('src_big' => sprintf($url, $userId, $i));
        }
        return $result;
    }

    public static function getFriends($userId)
    {
        $ids = array_fill(1, 1000, 1);
        $friendIds = array_rand($ids, mt_rand(10, 100));
        $result = array();
        foreach($friendIds as $friendId)
        {
            // avatar
            $avatar = PhotoUtility::getUrl().'/520052/ffffff.jpg&text=u%s_1';
            $avatar = sprintf($avatar, $friendId);

            $result[] = array
            (
                'uid' => $friendId,
                'name' => 'name_'.$friendId,
                'pic_big' => $avatar
            );
        }
        return $result;
    }
}

class PhpData
{
    static $data = array();

    public static function read($filename)
    {
        if(!isset(self::$data[$filename]))
        {
            $path = Yii::getPathOfAlias('application.data').'/'.$filename.'.php';
            self::$data[$filename] = require($path);
        }

        return self::$data[$filename];
    }
}